﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Models;
using System.Web.Script.Serialization;
using System.Web.SessionState; 

namespace Workflow.Project
{
    /// <summary>
    /// PJServer 的摘要说明
    /// </summary>
    public class PJServer : IHttpHandler
    {
        PJNodeManager objPJNodeManager = new PJNodeManager();
        ProjectMainManager objProjectMainManager = new ProjectMainManager();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["Type"];
            int workParentId = Convert.ToInt32(context.Request["WorkParentId"]);
            string pjId = context.Request["PJId"].ToString().Trim().ToUpper();
            string[] content = context.Request["SelectValue"].ToString().Split(',');
            string[] nodeName = context.Request["LabelValue"].ToString().Split(',');
            string[] completeDate = context.Request["TextValue"].ToString().Split(',');
            string[] number = context.Request["Number"].ToString().Split(',');
            try
            {
                if (type == "create" || type == "createTemp") 
                {
                    int count = objProjectMainManager.GetProjectInfoOfWorkParentId(-1, pjId, "PJMains");
                    if (count == 0) { context.Response.Write("该项目号还没有创建基础数据"); return; }
                    for (int i = 0; i < content.Length; i++)
                    {
                        PJNode PJNode = new PJNode()
                        {
                            WorkParentId = workParentId,
                            PJId = pjId,
                            Number = int.Parse(number[i])+1,
                            NodeName = nodeName[i],
                            CompleteDate = Convert.ToDateTime(completeDate[i]),
                            Content = content[i]
                        };
                        if (type == "create")
                        {
                            count = objPJNodeManager.GetIsNoPJId(pjId, "PJNodes");
                            if (count > 0) { context.Response.Write("该项目号已申请节点"); return; }
                            objPJNodeManager.InsertInfo(PJNode, "PJNodes");
                        }
                        else
                        {
                            PJNode.Reason = context.Request["Reason"].ToString();
                            objPJNodeManager.InsertInfo(PJNode, "PJNodesTemp");
                        }                 
                    }
                    if (type == "create"){ context.Response.Write("新建成功"); }
                    else { context.Response.Write("更改成功"); }
                    
                    
                }
                else if (type == "change" || type == "changeTemp")
                {
                    for (int i = 0; i < content.Length; i++)
                    {
                        PJNode PJNode = new PJNode()
                        {
                            WorkParentId = workParentId,
                            PJId = pjId,
                            Number = int.Parse(number[i]) + 1,
                            NodeName = nodeName[i],
                            CompleteDate = Convert.ToDateTime(completeDate[i]),
                            Content = content[i]
                        };
                        if (type == "change")
                        {
                            objPJNodeManager.UpdateInfo(PJNode, "PJNodes", null);
                        }
                        else
                        {
                            PJNode.Reason = context.Request["Reason"].ToString();
                            objPJNodeManager.UpdateInfo(PJNode, "PJNodesTemp", null);
                        }
                    }
                    context.Response.Write("更改成功");
                }
                
                
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message.ToString());
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}